<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>
        角色添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style type="text/css">
        .layui-form-switch {
            margin-top: 8px;
        }

    </style>
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <input type="hidden" name="id" th:value="${role!=null?role.id:''}">
        <div class="layui-form-item">
            <label for="roleName" class="layui-form-label">
                <span class="x-red">*</span>角色名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="roleName" name="roleName" required="" lay-verify="required"
                       th:value="${role!=null?role.roleName:''}"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="remarks" class="layui-form-label">
                <span class="x-red">*</span>角色说明
            </label>
            <div class="layui-input-inline">
                <input type="text" id="remarks" name="remarks" required=""
                       th:value="${role!=null?role.remarks:''}"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center;">

            <button th:if="${role == null}" class="layui-btn" lay-filter="add" lay-submit="">
                增加
            </button>
            <button th:if="${role != null}" class="layui-btn" lay-filter="update" lay-submit="">
                修改
            </button>

        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        form = layui.form;
        layer = layui.layer;

        //监听提交
        form.on('submit(add)', function (data) {
            //console.log(typeof data.field);
            //console.log(data);

            //发异步，把数据提交给后台处理
            $.ajax({
                url: "/role/addOrUpdateRole",
                data: JSON.stringify(data.field),
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if (data.code == 200) {
                        layer.alert(data.message, {icon: 6}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    } else {
                        layer.alert(data.message, {icon: 5}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                }
            });
            return false;
        });

        //监听修改
        form.on('submit(update)', function (data) {

            //发异步，把数据提交给后台处理
            $.ajax({
                url: "/role/addOrUpdateRole",
                data: JSON.stringify(data.field),
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    //console.log(data.message);
                    if (data.code == 200) {
                        layer.alert(data.message, {icon: 6}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    } else {
                        layer.alert(data.message, {icon: 5}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                        });
                    }
                }
            });
            return false;
        });
    });

</script>

</body>

</html>